package com.heima.admin.controller.v1;

import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理Controller", tags = "频道管理Controller", description = "频道管理Controller")
public class ChannelController {

    @Autowired
    private ChannelService channelService;

    @ApiOperation("条件分页查询代码")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody ChannelDto dto) {
        return this.channelService.findListByDto(dto);
    }

    @ApiOperation(value = "保存频道信息", notes = "保存时校验名称是否存在或者长度超过10")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody AdChannel adChannel) {
        return this.channelService.addChannel(adChannel);
    }

    @ApiOperation(value = "修改频道信息", notes = "修改时校验名字是否更改，若更改则判断长度以及数据库是否存在")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdChannel adChannel) {
        return this.channelService.update(adChannel);
    }

    @ApiOperation(value = "删除频道", notes = "若频道有效不能删除")
    @GetMapping("/del/{id}")
    public ResponseResult del(@PathVariable Integer id) {
        return this.channelService.delete(id);
    }
    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }
}
